<template>
  <div class="mobile-server-file common-mobile-view">
    <div class="file-header">
      <div class="header-left">
        <div class="header-tools">
          <i class="fa-solid fa-arrow-left" title="返回" @click="backFileClick"></i>
          <i class="fa-solid fa-sync" title="刷新" @click="fileStore.queryFileInfoList()"></i>
        </div>
        <div class="current-path">当前路径: {{ fileStore.currentFolder }}</div>
      </div>
    </div>
    <div class="file-list card-item">
      <div class="file-list-header">
        <div>文件名称</div>
        <div>文件大小</div>
        <div>创建日期</div>
      </div>

      <div class="file-list-content">
        <!-- 文件夹示例 -->
        <div v-for="(item, index) in fileStore.fileInfoList"
             :key=index class="file-item" @click="getFileClick(item)">
          <div class="file-name">
            <file-icons :name="item.name" :isFolder="item.type === '文件夹'"/>
            <span class="name">{{ item.name }}</span>
          </div>
          <span class="file-size">{{ item.size }}</span>
          <span class="file-creation-time">{{ item.createDate }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import FileIcons from 'file-icons-vue'
import { useAppStore } from '@/store/app.ts'
import { useFileStore } from '@/store/file.ts'
import { useRouter } from 'vue-router'
import { onMounted } from 'vue'
import { IFileInfo } from '@/types/file.ts'
import { ElMessage } from 'element-plus'

const fileStore = useFileStore()
const router = useRouter()
const appStore = useAppStore()

onMounted(() => {
  fileStore.queryFileInfoList()
})

const backFileClick = async () => {
  if (fileStore.pathHistory.length > 1) {
    fileStore.pathHistory.pop()
    fileStore.currentFolder = fileStore.pathHistory[fileStore.pathHistory.length - 1]
    await fileStore.queryFileInfoList()
  }
}

const getFileClick = async (file: IFileInfo) => {
  if (file.type === '文件夹') {
    fileStore.currentFolder = `${fileStore.currentFolder}/${file.name}`
    fileStore.pathHistory.push(fileStore.currentFolder)

    await fileStore.queryFileInfoList()
  } else if (fileStore.previewFileTypeList.includes(file.type)) {
    const fileUrl = fileStore.currentFolder.replace(fileStore.pathHistory[0], '')
    const url = `/admin-service/logs${fileUrl}/${file.name}`
    appStore.isShowMobileBack = true
    await router.push({ name: 'MobileHomeServerPreviewUrl', params: { url } })
  } else {
    ElMessage.error('暂不支持预览该文件')
  }
}
</script>

<style lang="scss">
.mobile-server-file {
  display: flex;
  flex-direction: column;
  gap: 10px;

  .file-header {
    background-color: var(--el-color-primary);
    color: #FFFFFF;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .header-left {
      display: flex;
      align-items: center;
      gap: 1.25rem;

      .header-tools {
        display: flex;
        gap: 1.25rem;

        i {
          cursor: pointer;
          transition: color 0.3s ease;
          font-size: 1.25rem;
        }

        i:hover {
          color: #e0f2fe;
        }
      }

      .current-path {
        font-weight: bold;
      }
    }
  }

  .file-list {
    .file-list-header {
      display: grid;
      grid-template-columns: 1fr 60px 120px;
      justify-items: center;
      align-items: center;
      gap: 5px;
      padding: 10px;
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
      background-color: var(--el-color-primary);
      color: #FFFFFF;
      font-weight: bold;
    }

    .file-list-content {
      max-height: calc(100vh - 220px);
      overflow: auto;
      display: flex;
      flex-direction: column;
      gap: 5px;

      .file-item {
        display: grid;
        grid-template-columns: 1fr 60px 120px;
        align-items: center;
        gap: 5px;
        padding: 10px;
        border-bottom: 1px solid #e0f2fe;

        .file-name {
          display: grid;
          grid-template-columns: 20px 1fr;
          align-items: center;
          gap: 3px;

          .name {
            font-weight: 600;
            color: #0c4a6e;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }

        .file-size,
        .file-creation-time {
          color: #64748b;
          font-size: 0.9rem;
        }
      }
    }
  }
}
</style>
